﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
//using Xoohoo.ModelValidation.Attributes;
using System.Collections.Generic;
using System.Linq;
using Kaiser.Models;

namespace Kaiser.Models.InputModels
{
    public class UserInput
    {
        public UserInput() { }
        public UserInput(int? userID, Guid userGroupID
            , UserStatus status
            , string username
            , string displayName
            , string email
            , IEnumerable<Guid> roles
            , IEnumerable<Guid> permissions
            )
        {
            UserID = userID;
            UserGroupID = userGroupID;
            Status = status;
            Username = username;
            DisplayName = displayName;
            Email = email;
            Roles = roles;
            Permissions = permissions;
        }

        [DisplayName("用户ID")]
        public int? UserID { get; set; }

        [Required(ErrorMessage = "所属用户组不能为空")]
        //[Guid(ErrorMessage = "所属用户组不正确")]
        [DisplayName("所属用户组")]
        public Guid UserGroupID { get; set; }

        [Required(ErrorMessage = "用户状态不能为空")]
        [Range(0, 3, ErrorMessage = "用户状态不正确")]
        [DisplayName("用户状态")]
        public UserStatus Status { get; set; }

        [Required(ErrorMessage = "用户名不能为空")]
        [StringLength(20, MinimumLength = 4, ErrorMessage = "用户名请保持在4-20个字符之间")]
        //[Slug(ErrorMessage = "用户名只能包含字母、数字、_和-，并以字母开头")]
        [DisplayName("用户名")]
        public string Username { get; set; }

        [Required(ErrorMessage = "昵称不能为空")]
        [StringLength(20, MinimumLength = 2, ErrorMessage = "昵称请保持在2-20个字符之间")]
        //[SlugWithChinese(ErrorMessage = "昵称只能包含中文字母、数字、_和-")]
        [DisplayName("昵称")]
        public string DisplayName { get; set; }

        [Required(ErrorMessage = "邮箱地址不能为空")]
        [StringLength(100, ErrorMessage = "邮箱地址请保持在100个字符之间")]
       // [Email(ErrorMessage = "邮箱地址格式不正确")]
        [DataType(DataType.EmailAddress, ErrorMessage = "邮箱地址格式不正确")]
        [DisplayName("邮箱地址")]
        public string Email { get; set; }

        public IEnumerable<Guid> Roles { get; set; }
        public IEnumerable<Guid> Permissions { get; set; }

        public static UserInput FromUser(User user)
        {
            if (user == null) return null;

            return new UserInput
            {
                UserID = user.UserID,
                //UserGroupID = user.UserGroup.UserGroupID,
                Username = user.UserName,
                DisplayName = user.DisplayName,
                Email = user.Email,
                Status = user.Status,
                Roles = user.Roles!=null?from r in user.Roles select r.RoleID:Enumerable.Empty<Guid>(),
                Permissions = user.Permissions != null ? from p in user.Permissions select p.PermissionID : Enumerable.Empty<Guid>(),
            };
        }

    }
}
